package com.qfedu.sem.dto;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.qfedu.sem.po.Bug;
import com.qfedu.sem.po.BugVersion;
import com.qfedu.sem.po.Project;

import wo.common.entity.WoPage;
import wo.common.util.WoConstant;
import wo.common.util.WoUtil;
import wo.department.po.Staff;

public class BugDto {
	private static Logger LOG = LogManager.getLogger(BugDto.class);

	public BugDto() {
	}

	public BugDto(Bug po) {
		// 故障提交节点字段：
		this.id = po.getId();
		this.title = po.getTitle();
		this.description = po.getDescription();
		if (po.getCommitter() != null) {
			this.committerId = po.getCommitter().getId();
			this.committerName = po.getCommitter().getName();
		}
		if (po.getVersion() != null) {
			this.versionId = po.getVersion().getId();
			this.versionNo = po.getVersion().getNo();
		}
		this.commitTime = po.getCommitTime();
		this.level = po.getLevel();
		this.rate = po.getRate();
		if (po.getProject() != null) {
			this.projectId = po.getProject().getId();
			this.projectName = po.getProject().getName();
		}
		if (po.getFixer() != null) {
			this.fixerId = po.getFixer().getId();
			this.fixerName = po.getFixer().getName();
		}
		if (po.getTester() != null) {
			this.testerId = po.getTester().getId();
			this.testerName = po.getTester().getName();
		}
		this.image = po.getImage();
		// 故障修复节点字段：
		this.fixRemark = po.getFixRemark();
		this.fixTime = po.getFixTime();
		if (po.getFixVersion() != null) {
			this.fixVersionId = po.getFixVersion().getId();
			this.fixVersionNo = po.getFixVersion().getNo();
		}
		this.confirmBug = po.getConfirmBug();
		// 测试验证节点字段
		this.testTime = po.getTestTime();
		this.isOk = po.getIsOk();
		if (po.getTestVersion() != null) {
			this.testVersionId = po.getTestVersion().getId();
			this.testVersionNo = po.getTestVersion().getNo();
		}
		// 当前节点
		this.currentNode = po.getCurrentNode();
		this.logs = BugLogDto.getDtos(po.getBugLogs());
		// 排序
		Collections.sort(logs, new Comparator<BugLogDto>() {

			@Override
			public int compare(BugLogDto o1, BugLogDto o2) {
				return (int) (o2.getProcessTime().getTime() - o1.getProcessTime().getTime());
			}
		});
	}

	private String id; // id

	private String title; // 标题

	private String description; // 描述

	private String level; // 级别

	private String rate; // 频度

	private String image; // 截图

	private String committerId; // 提交人

	private String committerName; // 提交人

	private Date commitTime; // 提交时间

	private String fixerId; // 修复人

	private String fixerName; // 修复人

	private String testerId; // 测试人

	private String testerName; // 测试人

	private String projectId; // 项目

	private String projectName; // 项目

	private String versionId; // 版本

	private String versionNo; // 版本

	@Temporal(TemporalType.TIMESTAMP)
	private Date fixTime; // 修复时间

	private Boolean confirmBug; // 确认故障

	private String fixRemark; // 修复说明

	private String fixVersionId; // 修复版本

	private String fixVersionNo; // 修复版本

	private Date testTime; // 测试时间

	private Boolean isOk; // 验证通过

	private String restRemark; // 验证说明

	private String testVersionId; // 验证版本

	private String testVersionNo; // 验证版本

	private List<BugLogDto> logs; // 故障日志

	private String currentNode; // 当前节点

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getRate() {
		return rate;
	}

	public void setRate(String rate) {
		this.rate = rate;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public String getCommitterId() {
		return committerId;
	}

	public void setCommitterId(String committerId) {
		this.committerId = committerId;
	}

	public String getCommitterName() {
		return committerName;
	}

	public void setCommitterName(String committerName) {
		this.committerName = committerName;
	}

	@JsonFormat(pattern = WoConstant.FORMAT_DATETIME, locale = "zh", timezone = "GMT+8")
	public Date getCommitTime() {
		return commitTime;
	}

	public void setCommitTime(Date commitTime) {
		this.commitTime = commitTime;
	}

	public String getFixerId() {
		return fixerId;
	}

	public void setFixerId(String fixerId) {
		this.fixerId = fixerId;
	}

	public String getFixerName() {
		return fixerName;
	}

	public void setFixerName(String fixerName) {
		this.fixerName = fixerName;
	}

	public String getTesterId() {
		return testerId;
	}

	public void setTesterId(String testerId) {
		this.testerId = testerId;
	}

	public String getTesterName() {
		return testerName;
	}

	public void setTesterName(String testerName) {
		this.testerName = testerName;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getVersionId() {
		return versionId;
	}

	public void setVersionId(String versionId) {
		this.versionId = versionId;
	}

	public String getVersionNo() {
		return versionNo;
	}

	public void setVersionNo(String versionNo) {
		this.versionNo = versionNo;
	}

	public Date getFixTime() {
		return fixTime;
	}

	public void setFixTime(Date fixTime) {
		this.fixTime = fixTime;
	}

	public Boolean getConfirmBug() {
		return confirmBug;
	}

	public void setConfirmBug(Boolean confirmBug) {
		this.confirmBug = confirmBug;
	}

	public String getFixRemark() {
		return fixRemark;
	}

	public void setFixRemark(String fixRemark) {
		this.fixRemark = fixRemark;
	}

	public String getFixVersionId() {
		return fixVersionId;
	}

	public void setFixVersionId(String fixVersionId) {
		this.fixVersionId = fixVersionId;
	}

	public String getFixVersionNo() {
		return fixVersionNo;
	}

	public void setFixVersionNo(String fixVersionNo) {
		this.fixVersionNo = fixVersionNo;
	}

	public Date getTestTime() {
		return testTime;
	}

	public void setTestTime(Date testTime) {
		this.testTime = testTime;
	}

	public Boolean getIsOk() {
		return isOk;
	}

	public void setIsOk(Boolean isOk) {
		this.isOk = isOk;
	}

	public String getRestRemark() {
		return restRemark;
	}

	public void setRestRemark(String restRemark) {
		this.restRemark = restRemark;
	}

	public String getTestVersionId() {
		return testVersionId;
	}

	public void setTestVersionId(String testVersionId) {
		this.testVersionId = testVersionId;
	}

	public String getTestVersionNo() {
		return testVersionNo;
	}

	public void setTestVersionNo(String testVersionNo) {
		this.testVersionNo = testVersionNo;
	}

	public String getCurrentNode() {
		return currentNode;
	}

	public void setCurrentNode(String currentNode) {
		this.currentNode = currentNode;
	}

	public List<BugLogDto> getLogs() {

		return logs;
	}

	public void setLogs(List<BugLogDto> logs) {
		this.logs = logs;
	}

	public static WoPage<BugDto> getPageData(List<Bug> content, long totalElements) {
		return new WoPage<BugDto>(getDtos (content), totalElements);
	}

	public static List<BugDto> getDtos(List<Bug> pos) {
		List<BugDto> dtos = new ArrayList<BugDto>();
		for (Bug po : pos) {
			dtos.add(new BugDto(po));
		}
		return dtos;
	}
	
	public Bug createPO() {
		Bug po = new Bug();
		// 设置PO主键id
		if (WoUtil.isEmpty(this.id)) {
			po.setId(java.util.UUID.randomUUID().toString());
		} else {
			po.setId(this.id);
		}
		// 设置PO属性title
		po.setTitle(this.title);
		// 设置PO属性description
		po.setDescription(this.description);
		// 设置PO属性level
		po.setLevel(this.level);
		// 设置PO属性rate
		po.setRate(this.rate);
		// 设置PO属性image
		po.setImage(this.image);
		// 设置PO属性commitTime
		po.setCommitTime(this.commitTime);
		// 设置PO属性fixTime
		po.setFixTime(this.fixTime);
		// 设置PO属性confirmBug
		po.setConfirmBug(this.confirmBug);
		// 设置PO属性fixRemark
		po.setFixRemark(this.fixRemark);
		// 设置PO属性testTime
		po.setTestTime(this.testTime);
		// 设置PO属性isOk
		po.setIsOk(this.isOk);
		// 设置PO属性restRemark
		po.setRestRemark(this.restRemark);
		// 设置PO属性currentNode
		po.setCurrentNode(this.currentNode);

		// 设置关系数据
		Staff committer = new Staff();
		if (!WoUtil.isEmpty(committerId)) {
			committer.setId(this.committerId);
			po.setCommitter(committer);
		}
		// 设置关系数据
		Staff fixer = new Staff();
		if (!WoUtil.isEmpty(fixerId)) {
			fixer.setId(this.fixerId);
			po.setFixer(fixer);
		}
		// 设置关系数据
		Staff tester = new Staff();
		if (!WoUtil.isEmpty(testerId)) {
			tester.setId(this.testerId);
			po.setTester(tester);
		}
		// 设置关系数据
		Project project = new Project();
		if (!WoUtil.isEmpty(projectId)) {
			project.setId(this.projectId);
			po.setProject(project);
		}
		// 设置关系数据
		BugVersion version = new BugVersion();
		if (!WoUtil.isEmpty(versionId)) {
			version.setId(this.versionId);
			po.setVersion(version);
		}
		// 设置关系数据
		BugVersion fixVersion = new BugVersion();
		if (!WoUtil.isEmpty(fixVersionId)) {
			fixVersion.setId(this.fixVersionId);
			po.setFixVersion(fixVersion);
		}
		// 设置关系数据
		BugVersion testVersion = new BugVersion();
		if (!WoUtil.isEmpty(testVersionId)) {
			testVersion.setId(this.testVersionId);
			po.setTestVersion(testVersion);
		}
		return po;
	}
}
